﻿using DataWasher.WebApi.EntitiesSQLServer;
using DataWasher.WebApi.Models;
using DataWasher.WebApi.Repositories.Base;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;

namespace DataWasher.WebApi.SqlServerRepositories
{
    public class StoreInfoRepository : SqlserverRepositoryBase<BaseStoreInfo>, IStoreInfoRepository
    {
        private readonly SqlServerDBContext _context;
        public StoreInfoRepository(SqlServerDBContext context) : base(context)
        {
            _context = context;
        }

        public Task<int> GetSourceDataCount()
        {
            throw new NotImplementedException();
        }

        public async Task<List<StoreData>> GetSourceDataPaged()
        {
            var result = from st in _context.BaseStoreInfo
                         join br in _context.BaseBrandInfo on st.BrandInfoId equals br.BrandInfoId
                         join dom in _context.SysDomain on st.DomainId equals dom.DomainId
                         join tr in _context.BaseStoreTagRelation on st.StoreId equals tr.StoreId into trJoin
                         from trjoin in trJoin.DefaultIfEmpty()
                         join bst in _context.BaseStoreTag on trjoin.StoreTagId equals bst.StoreTagId into bstJoin
                         from bstjoin in bstJoin.DefaultIfEmpty()
                         join bsty in _context.BaseStoreType on st.StoreTypeId equals bsty.StoreTypeId into bstyJoin
                         from bstyjoin in bstyJoin.DefaultIfEmpty()
                         where st.IsDeleted == false && br.IsDeleted == false
                         select new StoreData()
                         {
                             BrandNameCN = br.BrandNameCn,
                             DomainId = st.DomainId.ToString().ToLower(),
                             ParentDomainId = st.ParentDomainId.ToString().ToLower(),
                             DomainName = dom.DomainName,
                             DomainTag = dom.DomainTag,
                             Market = st.Market,
                             StoreId = st.StoreId.ToString().ToLower(),
                             StoreName = st.StoreName,
                             TagName = bstjoin.TagName,
                             TypeName = bstyjoin.TypeName
                         };

            return result.ToList();
        }
    }

}

